//
//  UIDeviceOrientationExample.swift
//  SwiftCode
//
//  Created by 于虎拴 on 2023/2/2.
//

import Foundation
import UIKit

final class UIDeviceOrientationExample: ListViewController {
  override func getItemCount() -> Int {
    data.count
  }
  
  override func getItem(_ indexPath: IndexPath) -> Item {
    data[indexPath.row]
  }
  
  private lazy var data: [Item] = [
    .init(title: "UIDeviceOrientation & UIInterfaceOrientation", description: "设备方向&界面方向", action: getOrientation),
    .init(title: "UIDevice#orientation = landscapeLeft", description: "设置横屏 landscapeLeft（真机有效）", action: landscapeLeft),
    .init(title: "UIDevice#orientation = landscapeLeft", description: "设置竖屏 portrait（真机有效）", action: portrait),
    .init(title: "restart App", description: "使用 exit(_: Int32)", action: { exit(0) }),
    .init(title: "restart App", description: "使用 abort()", action: { abort() }),
  ]
}

private func getOrientation() {
  print("UIDeviceOrientation:", UIApplication.shared.deviceOrientation.rawValue)
  print("UIDeviceOrientation.isPortrait:", UIApplication.shared.deviceOrientation.isPortrait)
  print("UIDeviceOrientation.isLandscape:", UIApplication.shared.deviceOrientation.isLandscape)
  print("")
  print("UIInterfaceOrientation:", UIApplication.shared.interfaceOrientation?.rawValue ?? -1)
  print("UIInterfaceOrientation.isPortrait:", UIApplication.shared.interfaceOrientation?.isPortrait ?? "")
  print("UIInterfaceOrientation.isLandscape:", UIApplication.shared.interfaceOrientation?.isLandscape ?? "")
}

private func landscapeLeft() {
  UIApplication.setOrientation(rotateTo: .landscapeLeft)
}

private func portrait() {
  UIApplication.setOrientation(rotateTo: .portrait)
}
